package cn.edu.xmu.oomall.comment.service;

import cn.edu.xmu.javaee.core.exception.BusinessException;
import cn.edu.xmu.javaee.core.model.ReturnNo;
import cn.edu.xmu.javaee.core.model.dto.UserDto;
import cn.edu.xmu.oomall.comment.dao.CommentDao;
import cn.edu.xmu.oomall.comment.dao.bo.Comment;
import cn.edu.xmu.oomall.comment.dao.bo.OrderItem;
import cn.edu.xmu.oomall.comment.dao.openfeign.OrderItemDao;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;


@Service
@Transactional(propagation = Propagation.REQUIRED)
@RequiredArgsConstructor
public class CommentService {
    private final static Logger logger = LoggerFactory.getLogger(CommentService.class);
    private final OrderItemDao orderItemDao;
    private final CommentDao commentDao;

    /**
     * 顾客新增评论
     * @param user 用户
     * @param comment 评论信息
     * @return 评论对象
     */
    public Comment createComment(Comment comment, UserDto user) {
        OrderItem orderItem = orderItemDao.findById(comment.getOrderItemId());
        Comment commentByOrderItemId = commentDao.retrieveByOrderItemId(orderItem.getId());
        if(Objects.nonNull(commentByOrderItemId)){
            throw new BusinessException(ReturnNo.COMMENT_EXISTED, String.format(ReturnNo.COMMENT_EXISTED.getMessage(), comment.getOrderItemId()));
        }
        return orderItem.createComment(comment, user);
    }

    /**
     * 通过商品id查找评论
     * @param id 商品id
     * @return 评论列表
     */
    public List<Comment> findCommentsByProductId(Long id, Integer page, Integer pageSize) {
        return commentDao.retrieveByProductId(id, page, pageSize);
    }


    /**
     * 管理员审核评论
     * @param comment 评论
     * @param user 用户
     * @return 审核后的评论
     */
    public Comment reviewComment(Comment comment, UserDto user) {
        Comment oldComment = commentDao.findById(comment.getId());
        return oldComment.reviewComment(comment, user);
    }

    /**
     * 商户回复评论
     * @param replyComment 回复评论
     * @param user 用户
     * @return 回复后的评论
     */
    public Comment replyComment(Comment replyComment, UserDto user) {
        Comment CommentRetrieveByPid = commentDao.retrieveByPid(replyComment.getPid());
        if(Objects.nonNull(CommentRetrieveByPid)){
            throw new BusinessException(ReturnNo.REPLY_COMMENT_EXISTED, String.format(ReturnNo.REPLY_COMMENT_EXISTED.getMessage(), replyComment.getPid()));
        }
        Comment comment = commentDao.findById(replyComment.getPid());
        return comment.replyComment(replyComment, user);
    }

    /**
     * 管理员查看评论
     * @param status 评论状态
     * @return 评论列表
     */
    public List<Comment> findCommentsByStatus(Byte status, Integer page, Integer pageSize) {
        return commentDao.retrieveByStatus(status, page, pageSize);
    }
}
